<template>
  <div class="exam-select">
    <a-carousel class="carousel" ref="carousel">
      <div v-for="(item, index) in examList" :key="item.id">
        <div class="type-one">
          <div class="name">{{ index + 1 }}、 {{ item?.question_name }}</div>
          <img v-if="item.question_type === 1" :src="item.question_url" alt="" class="question-img">
          <div class="select-list">
            <div class="item">
              <a-radio-group v-model:value="selectArr[index]" @change="handleRadioChange">
                <a-space :size="16" wrap>
                  <a-radio value="question_select_one">
                    <div class="radio-c">
                      <span>A、</span>
                      <img v-if="item.question_type === 0" :src="item.question_select_one" alt="" />
                      <span v-else>{{ item?.question_select_one }}</span>
                    </div>
                  </a-radio>
                  <a-radio value="question_select_two">
                    <div class="radio-c">
                      <span>B、</span>
                      <img v-if="item.question_type === 0" :src="item.question_select_two" alt="" />
                      <span v-else>{{ item?.question_select_two }}</span>
                    </div>
                  </a-radio>
                  <a-radio value="question_select_three">
                    <div class="radio-c">
                      <span>C、</span>
                      <img v-if="item.question_type === 0" :src="item.question_select_three" alt="" />
                      <span v-else>{{ item?.question_select_three }}</span>
                    </div>
                  </a-radio>
                  <a-radio value="question_select_four">
                    <div class="radio-c">
                      <span>D、</span>
                      <img v-if="item.question_type === 0" :src="item.question_select_four" alt="" />
                      <span v-else>{{ item?.question_select_four }}</span>
                    </div>
                  </a-radio>
                </a-space>
              </a-radio-group>
            </div>
          </div>
        </div>
      </div>
    </a-carousel>
    <div>
      <div style="text-align: center; margin-top: 24px">
        <a-space>
          <a-button v-if="position === 0" @click="cancelExam">取消</a-button>
          <a-button v-else type="primary" @click="prev()">上一题</a-button>
          <a-button v-if="position !== examList.length - 1" type="primary" @click="next()">下一题</a-button>
          <a-button v-else type="primary" @click="submitExam">交卷</a-button>
        </a-space>
      </div>
      <div style="text-align: center; margin-top: 24px">
        <a-checkbox v-model:checked="autoNext">答题后自动下一题</a-checkbox>
      </div>
      <div style="text-align: center; margin-top: 24px">
        <a-space>
          <a-button v-for="(item, index) in examList" :key="item.id" :type="index > position ? 'dashed' : 'primary'">{{ index + 1 }}</a-button>
        </a-space>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>

import { onMounted, ref } from 'vue'
import { fetchExamPaperDetailQuery } from '@/api/exam'
import { useRoute, useRouter } from 'vue-router'
import { message } from 'ant-design-vue';
import { fetchExamPaperSubmit } from '@/api/exam'

const route = useRoute()
const router = useRouter()

const id = ref<number>()
const selectArr = ref<any[]>([])

onMounted(() => {
  id.value = Number(route.query.id)
  getExamList()
})

const examList = ref<any[]>([])
const getExamList = async () => {
  const res = await fetchExamPaperDetailQuery({
    conditions: [
      {
        key: ' exam_paper_id',
        operator: 'eq',
        value: id.value
      }
    ]
  })
  examList.value = res.data.data
  selectArr.value = examList.value.map(() => void 0)
}

/**
 * 题目操作
 */
const carousel = ref()
const position = ref(0)
const autoNext = ref(true)
const prev = () => {
  if (position.value === 0) return
  position.value -= 1
  carousel.value.goTo(position.value)
}
const next = () => {
  if (position.value >= examList.value.length - 1) return
  position.value += 1
  carousel.value.goTo(position.value)
}
const handleRadioChange = () => {
  if (autoNext.value) {
    next()
  }
}

/**
 * 其余操作
 */
const cancelExam = () => {
  router.back()
}
const submitExam = async () => {
  const isSelectAll = selectArr.value.filter(item => item === undefined) // 是否答完了所有题
  if (isSelectAll && isSelectAll.length) return message.info('请先答完题再提交')
  const params = {
    exam_paper_id: id.value,
    answer_dtolist: selectArr.value.map((item, index) => ({ id: examList.value[index].id, answer: item }))
  }
  const res: any = await fetchExamPaperSubmit(params)
  if (res.data.code === '200') {
    message.success('交卷成功')
    router.back()
  } else {
    message.error(res.data.message)
  }  
}
</script>

<style scoped lang="scss">
.exam-select {
  background-color: #fff;
  .carousel {
    :deep(.slick-slide) {
      height: auto;
      overflow: hidden;
    }

    :deep(.slick-dots) {
      display: none !important;
    }
  }

  .type-one {
    padding: 40px 56px 0;

    .question-img {
      //width: 300px;
      height: 300px;
      margin-bottom: 16px;
    }

    .select-list {
      height: 88px;

      .item {
        height: 100%;
      }
    }

    .name {
      font-size: 20px;
      font-weight: bold;
      color: #000000;
      line-height: 44px;
      margin-bottom: 30px;
    }
  }

  .radio-c {
    display: flex;
    align-items: center;

    img {
      width: 88px;
      height: 88px;
    }
  }
}
</style>